package com.cans.web.pms;

import com.cans.dto.PageRes;
import com.cans.dto.ResDto;
import com.cans.dto.RoleGroupTreeDTO;
import com.cans.model.pms.SysRoleGroup;
import com.cans.service.pms.api.ISysRoleGroupService;
import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: cans
 * @date: 2018/5/1
 * @description: 角色组web层
 */
@Controller
@RequestMapping("roleGroup")
public class RoleGroupController {

    @Resource
    private ISysRoleGroupService sysRoleGroupService;

    /**
     * 跳转视图
     * @return
     */
    @GetMapping("toAdd/{parentId}")
    public String toAdd(@PathVariable String parentId,Model model){

        model.addAttribute("parentId",parentId);

        return "role_group/roleGroupAdd";
    }

    /**
     * 跳转视图
     * @return
     */
    @GetMapping("home")
    public String home(){

        return "role_group/roleGroupHome";
    }


    /**
     * 人员分页信息
     * @return
     */
    @GetMapping("/pageList/{parentId}")
    @ResponseBody
    public PageRes pageList(@PathVariable String parentId, Integer page, Integer limit){

        Pageable pageable = new PageRequest(page - 1,limit);
        //规格定义
        Specification<SysRoleGroup> specification = new Specification<SysRoleGroup>() {

            /**
             * 构造断言
             * @param root 实体对象引用
             * @param query 规则查询对象
             * @param cb 规则构建对象
             * @return 断言
             */
            @Override
            public Predicate toPredicate(Root<SysRoleGroup> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //所有的断言
                List<Predicate> predicates = new ArrayList<>();
                //添加断言
                if(StringUtils.isNotBlank(parentId)){
                    Predicate likeNickName = cb.equal(root.get("parentId").as(String.class),parentId);
                    predicates.add(likeNickName);
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
        return  sysRoleGroupService.findAll(specification,pageable);
    }

    /**
     * 新增
     * @param parentId
     * @param roleGroupCode
     * @param roleGroupName
     * @param description
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResDto add(String parentId,String roleGroupCode,String roleGroupName,String description){

        try {
            SysRoleGroup roleGroup = new SysRoleGroup();

            roleGroup.setParentId(parentId);
            roleGroup.setRoleGroupCode(roleGroupCode);
            roleGroup.setRoleGroupName(roleGroupName);
            roleGroup.setDescription(description);
            sysRoleGroupService.save(roleGroup);

        } catch (Exception e) {
            ResDto.fait(e.getMessage());
        }


        return ResDto.success();
    }

    /**
     * 加载角色组树
     * @return
     */
    @GetMapping("tree")
    @ResponseBody
    public ResDto loadRoleGroupTree(){

        List<SysRoleGroup> roleGroupList = sysRoleGroupService.findAll();

        List<RoleGroupTreeDTO> roleGroupTree = getChildren(roleGroupList, "-1");

        return  ResDto.success(roleGroupTree);
    }

    /**
     * 转化树结构
     * @param roleGroupList
     */
    private List<RoleGroupTreeDTO> getChildren(List<SysRoleGroup> roleGroupList, String parentId) {
        List<RoleGroupTreeDTO> childrenList = Lists.newArrayList();

        if(roleGroupList == null || roleGroupList.isEmpty()){
            return childrenList;
        }
        for (int i = 0; i < roleGroupList.size(); i++) {
            SysRoleGroup sysRoleGroup = roleGroupList.get(i);
            if(sysRoleGroup.getParentId().equals(parentId)){
                roleGroupList.remove(sysRoleGroup);
                i -- ;

                RoleGroupTreeDTO children = new RoleGroupTreeDTO().transfer(sysRoleGroup);
                children.setChildren(getChildren(roleGroupList,sysRoleGroup.getId()));
                childrenList.add(children);
            }
        }

        return childrenList;
    }
}
